package com.ruoyi.check.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.check.domain.Vacate;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.personnel.domain.Employee;
import com.ruoyi.personnel.mapper.EmployeeMapper;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.check.mapper.OvertimeMapper;
import com.ruoyi.check.domain.Overtime;
import com.ruoyi.check.service.IOvertimeService;

/**
 * 加班管理Service业务层处理
 *
 * @author tza
 * @date 2023-03-07
 */
@Service
public class OvertimeServiceImpl implements IOvertimeService {
    @Autowired
    private OvertimeMapper overtimeMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询加班管理
     *
     * @param otId 加班管理主键
     * @return 加班管理
     */
    @Override
    public Overtime selectOvertimeByOtId(Integer otId) {
        return overtimeMapper.selectOvertimeByOtId(otId);
    }

    /**
     * 查询加班管理列表
     *
     * @param overtime 加班管理
     * @return 加班管理
     */
    @Override
    public List<Overtime> selectOvertimeList(Overtime overtime) {
        System.out.println("员工姓名"+overtime.getEmpName());

        List<Overtime> overtimeList = overtimeMapper.selectOvertimeList(overtime);
        for (Overtime overtime1 : overtimeList) {
//            Employee employee = employeeMapper.selectEmployeeByEmpId(overtime1.getEmpId());
//            overtime1.setEmpName(employee.getEmpName());


            //计算加班时长
            //开始时间
            Date otStartTime = overtime1.getOtStartTime();
            //结束时间
            Date otEndTime = overtime1.getOtEndTime();

            //计算时间毫秒值
            long diff = otEndTime.getTime() - otStartTime.getTime();
            // 计算差多少小时
            //long hour = diff /60/60/1000 ;
            // 计算差多少分钟
            long min = diff /60/1000;

             String time =  ( min/60 ==0 ? "" : min/60+ "小时") +( min%60 == 0?"" : min%60+ "分钟");
             //String time =   min + "分钟" ;
            overtime1.setDifferTime(time);




            //调用若依工具类
//            String timeDistance = DateUtils.timeDistance(otEndTime, otStartTime);
//            overtime1.setDifferTime(timeDistance);

            //System.out.println("时间查"+timeDistance);




        }
        //return overtimeMapper.selectOvertimeList(overtime);
        return overtimeList;
    }

    /**
     * 新增加班管理
     *
     * @param overtime 加班管理
     * @return 结果
     */
    @Override
    public int insertOvertime(Overtime overtime) {
        return overtimeMapper.insertOvertime(overtime);
    }

    /**
     * 修改加班管理
     *
     * @param overtime 加班管理
     * @return 结果
     */
    @Override
    public int updateOvertime(Overtime overtime) {
        return overtimeMapper.updateOvertime(overtime);
    }

    /**
     * 批量删除加班管理
     *
     * @param otIds 需要删除的加班管理主键
     * @return 结果
     */
    @Override
    public boolean deleteOvertimeByOtIds(Integer[] otIds) {
        String[] arr = new String[otIds.length];
        for (int i = 0; i < otIds.length; i++) {
            System.out.println("删除的id值--" + otIds[i]);
            //根据id查询出时间
            Overtime overtime = overtimeMapper.selectOvertimeByOtId(otIds[i]);
            //数据库结束时间
            Date otEndTime = overtime.getOtEndTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String otEndTime01 = simpleDateFormat.format(otEndTime);
            System.out.println("数据库结束时间" + otEndTime);

            //当前时间
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String nowTime = formatter.format(date);
            //与当前时间做比较
            Date otEndTime02 = null;
            Date nowTime02 = null;
            try {
                otEndTime02 = simpleDateFormat.parse(otEndTime01);
                nowTime02 = simpleDateFormat.parse(nowTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            //是否超过
            boolean before = otEndTime02.before(nowTime02);
            if (before == true) {
                //删除
                arr[i] = "0";
            } else {
                arr[i] = "1";
            }
        }
        //数据
        String str = Arrays.toString(arr);
        if (str.contains("1")) {
            return false;
        } else {
            overtimeMapper.deleteOvertimeByOtIds(otIds);
            return true;
        }
        //return overtimeMapper.deleteOvertimeByOtIds(otIds);
    }

    /**
     * 删除加班管理信息
     *
     * @param otId 加班管理主键
     * @return 结果
     */
    @Override
    public int deleteOvertimeByOtId(Integer otId) {
        return overtimeMapper.deleteOvertimeByOtId(otId);
    }
}
